package com.tudouji.project.product.service.impl;

import com.tudouji.common.utils.StringUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.organization.mapper.ChannelMapper;
import com.tudouji.project.organization.service.IOrganizationService;
import com.tudouji.project.product.domain.Products;
import com.tudouji.project.product.mapper.ProductsMapper;
import com.tudouji.project.product.service.IBrandService;
import com.tudouji.project.product.service.IProductsService;
import com.tudouji.project.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ProductsServiceImpl implements IProductsService {

    @Autowired
    private ProductsMapper productsDao;


    @Autowired
    private IOrganizationService orgService;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private IBrandService brandService;

    @Override
    public List<Products> findProducePro(Products product) {
        return productsDao.findProducePro(product);
    }

    @Override
    public List<Products> findAllProducePro(Products product) {
        return productsDao.findAllProducePro(product);
    }

    @Override
    public List<Products> findCpProducePro(Products product) {
        return productsDao.findCpProducePro(product);
    }

    @Override
    public List<Products> getProducts(Integer type, String productflag) {
        List<Products> products;
        Products product = new Products();
        product.setType(type);
        if (StringUtils.equalsIgnoreCase(productflag, "5")) {
            product.setProductflag("5");
        }else{
            product.setProductflag("1");
        }
        product.setStats(new Integer[]{1, 3});
        products = getProductsByType(product);
        return products;
    }

    /**
     * 根据部门获取渠道产品
     *
     * @return
     */
    public List<Products> getProductsByDepartId(Products product) {
        return productsDao.getProByDepId(product);
    }


    /**
     * 根据产品类型获取产品
     *
     * @return
     */
    public List<Products> getProductsByType(Products product) {
        return productsDao.getProByType(product);
    }


    /**
     * ---------------------华丽的分割线------------------------
     */

    @Override
    public List<Products> getSpes(Long deptId, Integer type, Integer valuation, String productId) {
        List<Products> products = new ArrayList<>();
        Products product = new Products();
        product.setProductId(productId);
        product.setDepartId(deptId);
        product.setValuation(valuation);
        product.setType(type);
        if (deptId != null) {
            products = getSpesByDepartId(product);
            if (products.size() > 0) {
                return products;
            }
        }
        return getSpesByType(product);
    }

    @Override
    public AjaxResult getPrice(String speId, Integer currency, Integer valuation) {
        Products product = new Products();
        product.setSpeId(speId);
        product.setValuation(valuation);
        product.setCurrency(currency);

        product = getPricBySpeProId(product);
        if (product == null) {
            product = getConcessionByspeId(product);
        }
        return AjaxResult.success(product);
    }

    /**
     * 根据产品 币种从价格表中获取价格
     *
     * @return
     */
    public Products getPricBySpeProId(Products product) {
        return productsDao.getPricBySpeProId(product);
    }

    /**
     * 根据规格从产品表中获取优惠
     *
     * @return
     */
    public Products getConcessionByspeId(Products product) {
        return productsDao.getConcessionByspeId(product);
    }


    /**
     * 根据部门获取渠道规格
     *
     * @return
     */
    public List<Products> getSpesByDepartId(Products product) {
        return productsDao.getSpeByDepId(product);
    }


    /**
     * 根据产品类型获取规格
     *
     * @return
     */
    public List<Products> getSpesByType(Products product) {
        return productsDao.getSpeByType(product);
    }


}
